package com.atjava.nebula.core.exception.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ExpMsgResoureConfig {

	private static ExpMsgResoureConfig msgRes;	
	
	private Properties properties;
	
	public static ExpMsgResoureConfig getInstance() {
		if(msgRes == null) {
			return new ExpMsgResoureConfig() ;
		}
		return msgRes;
	}
	
	private ExpMsgResoureConfig() {
		properties = new Properties();

        InputStream stream = ExpMsgResoureConfig.class.getResourceAsStream("/exp_MsgResource.properties");

        if (stream == null) {
            throw new IllegalArgumentException("不能读取app.propertiesy文件, 请确定它在CLASSPATH下!");
        }

        try {
            properties.load(stream);
        } catch (IOException e) {
            IllegalStateException ex = new IllegalStateException("读取数据时发生错误!");
            ex.initCause(e);
            throw ex;
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                IllegalStateException ex = new IllegalStateException("关闭流时发生I/O错误!");
                ex.initCause(e);
                throw ex;
            }
        }
	}
	
	public String get(String key) {
        if (properties == null || key == null)
            return null;
        return (String) properties.get(key);
    }
	
	public String getMessage(String msgCode, String context) {
		
		if(context != null) {
			msgCode = msgCode + "." + context ;
		}
		return get(msgCode) ;
	}
}
